/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class User {

    private int userID;
    private String username;
    private String password;
    private String hoTen;
    private String diaChi;
    private String tinh;
    private String dienThoai;
    private String email;
    private Quyen quyen;

    public User(int userID, String userName, String hoTen, String diaChi, String tinh, String dienThoai, String email, Quyen quyen) {
        this.userID = userID;
        this.username = userName;
        this.hoTen = hoTen;
        this.diaChi = diaChi;
        this.tinh = tinh;
        this.dienThoai = dienThoai;
        this.email = email;
        this.quyen = quyen;


    }
    public User(){
    	
    }
    
    public User(int userid, String username, String email, Quyen quyen){
    	this.userID = userid;
    	this.username = username;
    	this.email = email;
    	this.quyen = quyen;
    }
    

    public User(String userName, String hoTen, String diaChi, String tinh, String dienThoai, String email, Quyen quyen) {
        this.username = userName;
        this.hoTen = hoTen;
        this.diaChi = diaChi;
        this.tinh = tinh;
        this.dienThoai = dienThoai;
        this.email = email;
        this.quyen = quyen;


    }

    public User(String username, String password, String hoTen, String diaChi,
			String tinh, String dienThoai, String email, Quyen quyen) {
		super();
		this.username = username;
		this.password = password;
		this.hoTen = hoTen;
		this.diaChi = diaChi;
		this.tinh = tinh;
		this.dienThoai = dienThoai;
		this.email = email;
		this.quyen = quyen;
	}
	public Quyen getQuyen() {
		return quyen;
	}

	@Override
	public String toString() {
		return "User [userID=" + userID + ", username=" + username
				+ ", password=" + password + ", hoTen=" + hoTen + ", diaChi="
				+ diaChi + ", tinh=" + tinh + ", dienThoai=" + dienThoai
				+ ", email=" + email + ", quyen=" + quyen + "]";
	}
	public void setQuyen(Quyen quyen) {
		this.quyen = quyen;
	}

	public boolean Save(User user) {
        boolean result = false;

        Connection con = ConnectDatabase.getConnection();
        try {
            Statement state = con.createStatement();
            String sql = "insert into user(username,password,name,address,provinceid,phone,email,roleid)" + " values('"
                    + user.getUsername() + "', '" + user.getPassword() + "', '" + user.getHoTen() + "', '" + user.getDiaChi() + "',"
                    + user.getTinh() + ", '" + user.getDienThoai() + "', '" + user.getEmail() + "'," + 0 + ")";
            int number = state.executeUpdate(sql);
            if (number > 0) {
                result = true;
            }
            con.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return result;
    }

    public boolean checklogin(String username, String password) {
        boolean result = false;
        Connection con = ConnectDatabase.getConnection();
        try {
            Statement state = con.createStatement();
            String sql = "select * from user where username='" + username + "' and password='" + password + "'";
            ResultSet resultSet = state.executeQuery(sql);
            while (resultSet.next()) {
                result = true;
            }
            con.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return result;
    }

    public static User login(String username, String pass) throws SQLException {
        Connection con = ConnectDatabase.getConnection();
        User user = null;
        Statement sta = con.createStatement();
        String query = "select * from user where username='" + username + "' and password='" + pass + "'";
        ResultSet result = sta.executeQuery(query);
        if (result.next()) {
            int userID = result.getInt(1);
            String userName = result.getString(2);
            String name = result.getString(4);
            String address = result.getString(5);
            String province = result.getString(6);
            String phone = result.getString(7);
            String email = result.getString(8);
            int roleID = result.getInt(9);
            Quyen quyen =  new Quyen(roleID);
            user = new User(userID, userName, name, address, province, phone, email, quyen);
        }
        con.close();
        return user;

    }

    public static boolean checkUserName(String username) {
        boolean kq_user = false;
        Connection con = ConnectDatabase.getConnection();
        try {
            Statement state = con.createStatement();
            String sql = "select * from user ";
            ResultSet result = state.executeQuery(sql);
            while (result.next()) {
                String checkUser = result.getString(1);
                if (checkUser.equals(username)) {
                    kq_user = true;
                    break;
                }
            }
            con.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        System.out.println(kq_user);
        return kq_user;
    }

    public int getUserID() {
        return userID;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    

    public String getHoTen() {
		return hoTen;
	}
	public void setHoTen(String hoTen) {
		this.hoTen = hoTen;
	}
	public String getDiaChi() {
		return diaChi;
	}
	public void setDiaChi(String diaChi) {
		this.diaChi = diaChi;
	}
	public String getTinh() {
		return tinh;
	}
	public void setTinh(String tinh) {
		this.tinh = tinh;
	}
	public String getDienThoai() {
		return dienThoai;
	}
	public void setDienThoai(String dienThoai) {
		this.dienThoai = dienThoai;
	}
	public static boolean addUser(String username, String pass, String name, String address, String province,String phone, String email, String roleid) {
        boolean res = false;
		Connection con = ConnectDatabase.getConnection();
        try {
            Statement sta = con.createStatement();
            String sql = "INSERT INTO user(username,password,name,address,provinceid,phone,email,roleid)"
                    + "VALUES('"+username+"'"
                    + ",'"+pass+"'"
                    + ",'"+name+"'"
                    + ",'"+address+"'"
                    + ","+province
                    + ",'"+phone+"'"
                    + ",'"+email+"'"
                    + ","+roleid+")";
            int i  = sta.executeUpdate(sql);            
            con.close();
            if(i > 0) res = true;
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return res;
    }
	public static ArrayList<User> searchUser(String username , int page, int numRecord) {
		if(page <= 0) page = 1;
        Connection con = ConnectDatabase.getConnection();
        ArrayList<User> list = new ArrayList<User>();
        try {
            Statement sta = con.createStatement();
            String sql = "SELECT * FROM user WHERE username like '%" + username + "%' ORDER BY userid DESC LIMIT "+numRecord+" OFFSET " + (page - 1) * numRecord;
            ResultSet re = sta.executeQuery(sql);
            while (re.next()) {
                int userid = re.getInt("userid");
                String user = re.getString("username");
                String email = re.getString("email");
                String role = re.getString("roleid");
                list.add(new User(userid, user, email, new Quyen(Integer.parseInt(role))));
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

	
	public static ArrayList<User> getAllUser(int numRecord, int page) {
		if(page <= 0) page = 1;
        Connection con = ConnectDatabase.getConnection();
        ArrayList<User> list = new ArrayList<User>();
        try {
            Statement sta = con.createStatement();
            String sql = "SELECT userid, username, email, roleid FROM user LIMIT  "+numRecord+" offset "+(page-1)*numRecord;
            ResultSet re = sta.executeQuery(sql);
            while (re.next()) {
                int userid = re.getInt("userid");
                String user = re.getString("username");
                String email = re.getString("email");
                String role = re.getString("roleid");
                list.add(new User(userid, user, email, new Quyen(Integer.parseInt(role))));
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }
	

	public static User getInfoUser(int userId) {
        Connection con = ConnectDatabase.getConnection();
        User user = null;
        try {
            Statement sta = con.createStatement();
            String sql = "SELECT * FROM user where userid="+userId;
            ResultSet re = sta.executeQuery(sql);
            while (re.next()) {
                int userid = re.getInt("userid");
                String username = re.getString("username");
//                String password = re.getString("password");
                String name = re.getString("name");
                String address = re.getString("address");
                String phone = re.getString("phone");
                String email = re.getString("email");
                String role = re.getString("roleid");
                String provinceid = re.getString("provinceid");
                user = new User(userid, username, name, address, provinceid, phone, email, new Quyen(Integer.parseInt(role)));
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return user;
    }

    public static boolean deleteUser(int userid) {
    	boolean res = false;
        Connection con = ConnectDatabase.getConnection();
        try {
            Statement sta = con.createStatement();
            String sql = "DELETE FROM user WHERE userid = " + userid;
            int i = sta.executeUpdate(sql);
            con.close();
            if(i>0) res = true;
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }

    public static int countUser() {
        Connection con = ConnectDatabase.getConnection();
        int count = 0;
        try {
            Statement sta = con.createStatement();
            String sql = "SELECT COUNT(*) FROM user";
            ResultSet re = sta.executeQuery(sql);
            if (re.next()) {
                count = re.getInt(1);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return count;
    }


    public static int countUserSearch(String username) {
        Connection con = ConnectDatabase.getConnection();
        int count = 0;
        try {
            Statement sta = con.createStatement();
            String sql = "SELECT COUNT(*) FROM user where username like '%" + username + "%' ";
            ResultSet re = sta.executeQuery(sql);
            if (re.next()) {
                count = re.getInt(1);
                System.out.println(count);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return count;
    }

    
	
	public static void main(String[] args) throws SQLException {
		User a = new User();
		
//		System.out.println(User.countUserSearch("c"));
		System.out.println("Aloalo"+("/SHOPCH4/Admin".contains("Admin")));
		
		
	}
}
